<?php

class TaskTable extends Doctrine_Table
{
  public function getResourceCostPerMilestone($projectId){
    $resourceCostPerMilestone = Doctrine_Query::create()
	  ->select('t.milestoneid, m.name AS milestoneName, count(Distinct t.taskid) AS taskCount, count(Distinct r.resourceid) AS resourceCount, sum(IFNULL(r.rate,'.sfConfig::get('app_min_cost_per_resource').')*(IFNULL(hour(timediff(estimatedenddate,startdate)),0)+(IFNULL(minute(timediff(estimatedenddate,startdate))/60,0)))*'.sfConfig::get('app_working_hours_per_day').') AS milestoneCost')
      ->from('Task t')
	  ->leftJoin('t.Resource r')
	  ->leftJoin('t.Milestone m')
	  ->where('m.projectid = ?', $projectId)
	  ->groupBy('t.milestoneid, m.name')
	  ->execute();

	return $resourceCostPerMilestone;
  }
  public function getResourceCostPerTask($projectId){
    $resourceCostPerTask = Doctrine_Query::create()
	  ->select('t.taskid, t.milestoneid, t.startdate AS startDate, t.estimatedenddate AS endDate, r.resourceid AS resourceId, r.name AS resourceName, IFNULL(r.rate,'.sfConfig::get('app_min_cost_per_resource').')*'.sfConfig::get('app_working_hours_per_day').' AS resourceCost')
      ->from('Task t')
	  ->leftJoin('t.Resource r')
	  ->leftJoin('t.Milestone m')
	  ->where('m.projectid = ?', $projectId)
	  ->orderBy('t.startdate ASC')
	  ->execute();

	return $resourceCostPerTask;
  }
  public function addTasksQuery(Doctrine_Query $q = null)
  {
  	if (is_null($q))
    {
      $q = Doctrine_Query::create()
        ->from('task t');
    }
	$alias = $q->getRootAlias();	
    return $q;
  }
  
  public function getProjectsOfLoggedInUser()
  {
    return SmartManager::getProjectsOfLoggedInUser();
  }
}